//
//  SqlResultSet.m
//  iphone_test
//
//  Created by whlsxl on 10-5-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SqlResultSet.h"

#define SQL_RESULT_SET_SHOW_LOG

@implementation SqlResultSet


#pragma mark create

- (id)init{
	[super init];
	now = 0;
	data_index = -1;
	data = [[NSMutableArray alloc] init];
	total_att = 0;
	att_name_list = [[NSMutableArray alloc] init];
	[self addNewRecord];
	return self;
}


- (void)addNewRecord{
	[data addObject:[[NSMutableArray alloc] init]];
}
- (void)addItem:(NSString *)item{
	if (now >= total_att){
		[self addNewRecord];
		now = 0;
	}
	[[data lastObject] addObject:item];
	now ++;

}

- (void)setNameAtt:(NSString *)att_name{
	[att_name_list addObject:att_name];
	total_att ++;
}

#pragma mark use

#pragma mark get methed


- (BOOL)next{
	data_index ++;
	if ([data count] <= data_index) {
		return NO;
	}
	return YES;
}

- (NSString *)getStringByIndex:(int) index{
	if ([self isOut]) {
		return nil;
	}
	if (index < total_att) {
		return [[data objectAtIndex:data_index] objectAtIndex:index];
	}
	[self showLog:@"SqlResultSet getStringByIndex methed \"index error\""];
	return nil;
}
- (NSString *)getStringByAtt:(NSString*) att{
	int index_temp = [self getIndexByAtt:att];
	if(index_temp == -1){
		[self showLog:@"SqlResultSet getStringByAtt methed \"don't have this att\""];
		return nil;
	}
	else {
		return [self getStringByIndex:index_temp];
	}
}

- (int)getIntByIndex:(int) index{
	if ([self isOut]) {
		return 0;
	}
	if (index < total_att) {
		return [[[data objectAtIndex:data_index] objectAtIndex:index] integerValue];
	}
	[self showLog:@"SqlResultSet getIntByIndex methed \"index error\""];
	return 0;
}
- (int)getIntByAtt:(NSString *) att{	
	int index_temp = [self getIndexByAtt:att];
	if(index_temp == -1){
		[self showLog:@"SqlResultSet getIntByAtt methed \"don't have this att\""];
		return 0;
	}
	else {
		return [self getIntByIndex:index_temp];
	}
}

- (unsigned long int)getULongByIndex:(int) index{
	if ([self isOut]) {
		return 0;
	}
	if (index < total_att) {
		return [[[data objectAtIndex:data_index] objectAtIndex:index] integerValue];
	}
	[self showLog:@"SqlResultSet getULongByIndex methed \"index error\""];
	return 0;
}
- (unsigned long int)getULongByAtt:(NSString *) att{
	int index_temp = [self getIndexByAtt:att];
	if(index_temp == -1){
		[self showLog:@"SqlResultSet getULongByAtt methed \"don't have this att\""];
		return 0;
	}
	else {
		return [self getULongByIndex:index_temp];
	}
}

- (int)getIndexByAtt:(NSString *) att{
	int cycle = 0;
	for (NSString *s in att_name_list){
		if ([s isEqualToString:att]) {
			return cycle;
		}
		cycle ++;
	}
	return -1;
}

- (BOOL)isOut{
	if (data_index < 0 || data_index > [data count]) {
		return YES;
	}
	return NO;
	
}
- (void)release{
	for (NSMutableArray *a in data){
		[a release];
	}
	[data release];
	[att_name_list release];
	[super release];
}
#pragma mark others
- (void)showLog:(NSString *)log{
#ifdef SQL_RESULT_SET_SHOW_LOG
	NSLog(@"%@",log);
#endif
}
//+ (unsigned long int)parseLong:(NSString *)string{return 1;}

@end
